<?php

/**
 * This is the model class for table "post_image".
 *
 * The followings are the available columns in table 'post_image':
 * @property string $id
 * @property string $post_id
 * @property string $reply_id
 * @property string $user_id
 * @property string $image
 * @property integer $is_cover
 *
 * The followings are the available model relations:
 * @property PostReply $reply
 * @property Post $post
 */
class PostImage extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PostImage the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'post_image';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('post_id, user_id, image', 'required'),
			array('is_cover', 'numerical', 'integerOnly'=>true),
			array('post_id, reply_id, user_id', 'length', 'max'=>10),
			array('image', 'length', 'max'=>300),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, post_id, reply_id, user_id, image, is_cover', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'reply' => array(self::BELONGS_TO, 'PostReply', 'reply_id'),
			'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'post_id' => 'Post',
			'reply_id' => 'Reply',
			'user_id' => 'User',
			'image' => 'Image',
			'is_cover' => 'Is Cover',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('post_id',$this->post_id,true);
		$criteria->compare('reply_id',$this->reply_id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('is_cover',$this->is_cover);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function afterSave(){
        Post::model()->updateImageCount($this->post_id);
        return parent::afterSave();
    }
    
    public function getUrl($type = 'large', $isPath = false){
        $postPath = Post::model()->getImagePath($this->post_id);
        if($this->reply_id){
            $postPath .= "{$this->reply_id}/";    
        }
        return (!$isPath ? Yii::app()->getBaseUrl(true).'/' : '') . "{$postPath}{$this->image}_{$type}.jpg";
    }
    
        
    public function deleteFile(){
        Myext::deleteFile($this->getUrl('large', true));    
        Myext::deleteFile($this->getUrl('small', true));      
    } 
    
    public function getNextId(){
        $postImage = $this->find(array(
            'select' => 'id',
            'order' => 'id DESC',
            'limit' => 1,
        ));
        return $postImage['id'] + 1;    
    }
}